import Vue from 'vue'
import VueRouter from 'vue-router'

import Vip from '../components/vip/Vip.vue'
import Home from '../components/home/Home.vue'
import Menu from '../components/menu/Menu.vue'
import SetMeal from '../components/setMeal/setMeal.vue'
import Slideshow from '../components/slideshow/Slideshow.vue'
import Users from '../components/users/users.vue'

import Index from '../views/index/index.vue'
import Login from '../views/login/login.vue'
import { Message } from 'element-ui';
import api from '../http/api'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index/home'
  },
  {
    path: "/index",
    component: Index,
    name: "Index",
    beforeEnter: async (to, from, next) => {
      if (localStorage.token) {
        const data = await api.users.getUsers()
        // console.log(data);
        if (data.code === 200) {
          next()
        }
      } else {
        Message({
          message: '你还未登录，请登录',
          type: 'warning'
        });
        next('/login')
      }
    },
    children: [
      {
        path: "vip",
        name: "Vip",
        component: Vip
      },          

      {
        path: "home",
        name: "Home",
        component: Home
      },
      {
        path: "menu",
        name: "Menu",
        component: Menu
      },
      {
        path: "setMeal",
        name: "SetMeal",
        component: SetMeal
      },
      {
        path: "slideshow",
        name: "Slideshow",
        component: Slideshow
      },
      {
        path: "users",
        name: "Users",
        component: Users
      },


    ]
  },
  {
    path: "/login",
    component: Login,
    name: "Login"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
